/*
 * iShopPlace Project.
 */
package ishopplace.model.dao.hibernate;

import ishopplace.model.businessobject.Order;
import ishopplace.model.dao.OrderDao;
import ishopplace.model.dao.helper.Sequence;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * The Hibernate implementation of the <code>OrderDao</code>
 * 
 * @author <a href="mailto:derek@derekshen.com">Derek Y. Shen</a>
 * @see OrderDao
 */
public class OrderDaoHibernateImpl extends HibernateDaoSupport implements OrderDao {
    private static final String ORDER_SEQUENCE_NAME = "orderSeq";
    
	public void saveOrder(Order order) {
	    order.setOrderId(this.getSeqNum(ORDER_SEQUENCE_NAME));
		this.getHibernateTemplate().save(order);
	}
	
	public Order getOrder(String orderId) {
		return (Order)this.getHibernateTemplate().load(Order.class, orderId);
	}
	
	private int getSeqNum(String name) {
	    int seqNum = -1;
	    Sequence seq = (Sequence)this.getHibernateTemplate().load(Sequence.class, name);
	    seqNum = seq.getSeqnum();
	    seq.increment();
	    this.getHibernateTemplate().update(seq);
	    return seqNum;
	}
}
